package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.R.integer;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FragmentEx extends Activity {
	public final static String TAG = "FragmentCycle";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmenttest);
	}
	
	public static class CounterFragment extends Fragment {
		TextView textCounter;
		int mStart;
		
		public static CounterFragment newInstance(int start){
			CounterFragment CF = new CounterFragment();
			CF.mStart = start;
			return CF;
		}
		
	
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			Log.d(TAG, "				Fragment - onActivityCreated");
			// TODO Auto-generated method stub
			super.onActivityCreated(savedInstanceState);
		}

		@Override
		public void onAttach(Activity activity) {
			Log.d(TAG, "				Fragment - onAttach");
			// TODO Auto-generated method stub
			super.onAttach(activity);
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			Log.d(TAG, "				Fragment - onCreate");
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG, "				Fragment - onCreateView");
			
			View root = inflater.inflate(R.layout.ch20_counterfragment, container, false);
			
			Button btnIncrease = (Button) root.findViewById(R.id.ch20_counterfragment_btn);
			textCounter = (TextView) root.findViewById(R.id.ch20_counterfragment_counter_tv);
			
			
			// 프레임조정(가로,세로화면)될때 값 저장해서 다시 뿌려줄수 있도록.
			if(savedInstanceState != null){
				textCounter.setText(Integer.toString(savedInstanceState.getInt("counter")));
			}
			

			Bundle bundle = getArguments();
			if(bundle != null){
				textCounter.setText(Integer.toString(bundle.getInt("start")));
			}
			
			btnIncrease.setOnClickListener(new Button.OnClickListener(){
				@Override
				public void onClick(View v) {
					int count = Integer.parseInt(textCounter.getText().toString());
					textCounter.setText(Integer.toString(count + 1));
				}
			});
			return root;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			int a = Integer.parseInt(textCounter.getText().toString());
			outState.putInt("counter", a);
		}
		
		@Override
		public void onDestroy() {
			Log.d(TAG, "				Fragment - onDestroy");
			// TODO Auto-generated method stub
			super.onDestroy();
		}

		@Override
		public void onDestroyView() {
			Log.d(TAG, "				Fragment - onDestroyView");
			// TODO Auto-generated method stub
			super.onDestroyView();
		}

		@Override
		public void onPause() {
			Log.d(TAG, "				Fragment - onPause");
			// TODO Auto-generated method stub
			super.onPause();
		}

		@Override
		public void onResume() {
			Log.d(TAG, "				Fragment - onResume");
			// TODO Auto-generated method stub
			super.onResume();
		}

		@Override
		public void onStart() {
			Log.d(TAG, "				Fragment - onStart");
			// TODO Auto-generated method stub
			super.onStart();
		}

		@Override
		public void onStop() {
			Log.d(TAG, "				Fragment - onStop");
			// TODO Auto-generated method stub
			super.onStop();
		}
		
	}
}
